package com.stormrage.edu.serv.index.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.stormrage.edu.dao.index.IdxTrialDao;
import com.stormrage.edu.serv.index.IdxTrialServ;
import com.stormrage.entity.index.IdxTrial;
import com.stormragetech.core.entity.UploadVideoRep;
import com.stormragetech.core.exception.StormrageTechException;
import com.stormragetech.core.util.FileUtil;

/**
 * @Title: IdxTrialServImpl.java
 * @Package com.stormrage.edu.serv.Index.impl
 * @Description: 试看课件 Service
 * @author www.stormragetech.com
 * @date 2016年4月17日
 * @version V1.0
 */
@Service
public class IdxTrialServImpl implements IdxTrialServ {

	private static final Logger logger = LoggerFactory.getLogger(IdxTrialServImpl.class);

	@Value("#{constants['path.ffmpeg']}")
	private String ffmpegPath;

	@Autowired
	private IdxTrialDao idxTrialDao;

	@Override
	public Integer create(final IdxTrial arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Create 】- 开始运行 ");

		Integer index = idxTrialDao.create(arg0);

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Create 】- 完成运行 ");
		return index;

	}

	@Override
	public void update(final IdxTrial arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Update 】- 开始运行 ");

		idxTrialDao.update(arg0);

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Update 】- 完成运行 ");

	}

	@Override
	public void delete(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Delete 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		idxTrialDao.delete(map);

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 Delete 】- 完成运行 ");

	}

	@Override
	public IdxTrial queryById(final Integer arg0) throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 开始运行 ");

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", arg0);

		IdxTrial entity = idxTrialDao.queryById(map);

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 完成运行 ");
		return entity;

	}

	@Override
	public List<IdxTrial> queryList() throws StormrageTechException {

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 开始运行 ");

		List<IdxTrial> array = idxTrialDao.queryList();

		logger.debug(" 【 Serv 】-【 IdxTrialServ 】-【 QueryList 】- 完成运行 ");
		return array;

	}

	@Override
	public UploadVideoRep upload(final MultipartFile arg0, final String arg1) throws StormrageTechException {

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Upload 】- 【 STARTED 】");

		UploadVideoRep entity = null;

		String path = FileUtil.initUploadPath(arg1, "trial");
		String fileName = FileUtil.saveFile(arg0, path, "trial");

		fileName = FileUtil.initFileNameNoSuffix(fileName);

		logger.debug(" path " + arg1);

		entity = FileUtil.saveImageForVideo(fileName, arg1, ffmpegPath, "trial");

		logger.debug(" 【 Service 】- 【 EduVideoServ 】- 【 Upload 】- 【 COMPLETED 】");
		return entity;

	}

}
